package edu.ynmd.lzk.action;

import edu.ynmd.lzk.dao.AreaDao;
import edu.ynmd.lzk.dao.CityDao;
import edu.ynmd.lzk.dao.ProvinceDao;
import  edu.ynmd.lzk.dao.StudentDao;

import edu.ynmd.lzk.model.StudentEntity;
import edu.ynmd.lzk.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import  java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
public class PublicAction {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private CityDao cityDao;

    @Autowired
    private AreaDao areaDao;






    @GetMapping("testDao")
    @ResponseBody
    public boolean testDao() throws Exception {
        //添加
       // StudentEntity student=new StudentEntity();
       //student.setStunum("201621447101");
       // student.setName("姓名");
        //student.setSex("F");
       //student.setBirthday(new Timestamp(System.currentTimeMillis()));
       //student.setNativeplace("云南玉溪");
       //studentDao.save(student);

        //修改
       //StudentEntity student=new StudentEntity();
        //student.setStudentid("67cee76f-72d9-4aa6-beb1-fdfef8aef380");
        //student.setStunum("201621447102");
       // student.setName("姓名修改");


        //查询单个
       //StudentEntity student=studentDao.findOne("67cee76f-72d9-4aa6-beb1-fdfef8aef380");
       //student.setSex("M");
      // student.setBirthday(new Timestamp(System.currentTimeMillis()));
        //student.setNativeplace("红塔区");


        //删除

        StudentEntity student=new StudentEntity();
        student.setStudentid("67cee76f-72d9-4aa6-beb1-fdfef8aef380");
        studentDao.save(student);
        return true;
    }
    @PostMapping("saveStudentInfo")
    @ResponseBody
    public Map saveStudentInfo(@RequestBody StudentVo studentVo) throws Exception {
        Map m=new HashMap();
        String sf=studentVo.getSfzh().substring(0,2);
        String sfname=provinceDao.getProvinceNameBySfCode(sf+"0000");

        String zs=studentVo.getSfzh().substring(2,4);
        String zsname=cityDao.getCityNameByZsCode(sf+zs+"00");

        String qx=studentVo.getSfzh().substring(4,6);
        String qxname=areaDao.getAreaNameByQxCode(sf+zs+qx);

        String csrq=studentVo.getSfzh().substring(6,14);
        Timestamp csrqtime=this.getBirthTimeByStr(csrq);
        String xb=studentVo.getSfzh().substring(16,17);
        String xbname=this.getXbByCode(xb);

        StudentEntity student=new StudentEntity();
        student.setNativeplace(sfname+zsname+qxname);
        student.setBirthday(csrqtime);
        student.setSex(xbname);
        student.setName(studentVo.getXm());
        student.setStunum(studentVo.getXh());
        studentDao.save(student);

        m.put("province",sfname);
        m.put("city",zsname);
        m.put("area",qxname);
        m.put("csrq",csrqtime);
        m.put("xb",xbname);

        return m;
    }
    private String getXbByCode(String code){
        int sexi=Integer.parseInt(code);
        if(sexi%2==0){
            return "女";
        }
        else{
            return "男";
        }
    }
    private Timestamp getBirthTimeByStr(String brithStr){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(brithStr, pos);
        return new Timestamp( strtodate.getTime());

    }

}
